<?php

class Record extends AppModel {

    public $name = 'Record';
    public $useTable = "records";
    public $primaryKey = 'Id';
    

    /**
     * 
     */
    public function getRecordConditions($params,$offset = 0,$limit = 10,$count = false,$pagination = true){
        $conditions = "";
        $binds = array();
        //
        if(isset($params['Camera']['Id'])){
            $conditions .= " AND cameras.Id = :Id ";
            $binds[':Id'] = $params['Camera']['Id'];
        }
        //
        if(isset($params['Camera']['CustId']) && $params['Camera']['CustId'] != '0'){
            $conditions .= " AND cameras.CustId = :CustId ";
            $binds[':CustId'] = $params['Camera']['CustId'];
        }
        //
        if(isset($params['Camera']['BuildId']) && $params['Camera']['BuildId'] != '0'){
            $conditions .= " AND cameras.BuildId = :BuildId ";
            $binds[':BuildId'] = $params['Camera']['BuildId'];
        }
        //
        if(isset($params['Camera']['CamName']) && strlen($params['Camera']['CamName']) > 0){
            $conditions .= " AND cameras.CamName like :CamName ";
            $binds[':CamName'] = '%'.$params['Camera']['CamName'].'%';
        }
        //
        $user = CakeSession::read("Auth.User");
        if(!$user['IsAdmin']){
            $conditions .= " AND cameras.CustId = :cus ";
            $binds[':cus'] = $user['Id'];
        }
        
        
        if($count){
            $sql = "Select count(records.Id) as count FROM records,cameras,buildings,customers where records.CamId = cameras.Id AND cameras.CustId = customers.Id AND cameras.BuildId = buildings.Id ";
            $return = $this->getDataSource()->fetchAll($sql.$conditions,$binds);
            $return = $return[0][0]['count'];
        }elseif($pagination){
            $sql = "Select * FROM records,cameras,buildings,customers where records.CamId = cameras.Id AND cameras.CustId = customers.Id AND cameras.BuildId = buildings.Id ";
            $return = $this->getDataSource()->fetchAll($sql.$conditions."ORDER BY records.CrtDt DESC limit ".$offset.",".$limit,$binds);
        }else{
            $sql = "Select * FROM records,cameras,buildings,customers where records.CamId = cameras.Id AND cameras.CustId = customers.Id AND cameras.BuildId = buildings.Id ";
            $return = $this->getDataSource()->fetchAll($sql.$conditions.'ORDER BY records.CrtDt DESC');
        }
        return $return;
        
    }
    public function getFileById($id){
        $sql='Select Record.*, cameras.CamName,buildings.BuildName, customers.CustName  FROM records as Record,cameras,buildings,customers '
                . 'where Record.CamId = cameras.Id AND cameras.CustId = customers.Id AND cameras.BuildId = buildings.Id and Record.Id= :Id';
        $arr['Id']=$id;
         $return = $this->getDataSource()->fetchAll($sql,$arr);
         return $return[0];
    }

        /**
     * 
     */
    public function getNextFile($code){
        $i = $this->find('count',array('conditions' => array('Record.CamId' => $code)));
        return $i+1;
        
    }

}